package com.warm.mini.wework.controller;

import com.alibaba.fastjson.JSON;
import com.warm.mini.wework.form.ConfigForm;
import com.warm.mini.wework.form.PosterForm;
import com.warm.mini.wework.form.PosterOption;
import com.warm.mini.wework.model.JsonConfig;
import com.warm.mini.wework.model.JsonConfig.Poster;
import com.warm.mini.wework.util.ImageUtil;
import com.warm.mini.wework.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.net.URI;
import java.nio.file.Path;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.UUID;

/**
 * @author warmchuang
 * @date 2023/12/6 10:47
 * @desc
 */
@RestController
@Slf4j
public class PosterController {

    public static JsonConfig jsonConfig;

    private static final String JSON_CONFIG_PATH = "config.json";

    public static JsonConfig getConfig(){
        if(jsonConfig == null){
            jsonConfig = JsonUtil.readJsonFile(JSON_CONFIG_PATH, JsonConfig.class);
        }
        return jsonConfig;
    }

    public static void refreshConfig(){
        jsonConfig = JsonUtil.readJsonFile(JSON_CONFIG_PATH, JsonConfig.class);
    }


    @GetMapping("/poster/init")
    public Object posterInit() {
        JsonConfig config = getConfig();
        List<PosterOption> options = new ArrayList<>();
        config.getPosters().forEach(p -> {
            PosterOption option = new PosterOption();
            option.setType(p.getType());
            option.setName(p.getName());
            option.setSourceImg(p.getSourceImg());
            if(p.getType().equals(9999)){ // 倒计时逻辑
                int dayOfMonth = LocalDate.now().getMonth().maxLength() - LocalDate.now().getDayOfMonth();
                LocalDate.now().getMonth().getValue();
                option.setName(String.format(option.getName(), dayOfMonth));
                option.setSourceImg(String.format(option.getSourceImg(), dayOfMonth));
            }
            // 页面路径处理 多个 static 路径访问不到
            option.setSourceImg(option.getSourceImg());
            options.add(option);
        });
        return options;
    }

    @PostMapping("/poster/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("type") Integer type) {
        try{
            JsonConfig config = getConfig();
            Poster poster = config.getPosterMap().get(type);

            byte[] bytes = ImageUtil.addWatermark(file.getInputStream(), poster);

            String filename = "/static/temp/" + UUID.randomUUID() + ".png";
            //ImageUtil.addWatermark(file.getInputStream(), poster, filename);
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e){
            e.printStackTrace();
        }
        return "poster";
    }
}
